//
//  SetUpViewController.m
//  MyNewProject
//
//  Created by sun on 2019/1/29.
//  Copyright © 2019 sun. All rights reserved.
//

#import "SetUpViewController.h"
#import "MyPersonInfoVC.h"
#import "RealNameViewController.h"
#import "PassWordModifyVC.h"
#import "AddressListVC.h"
#import "FeedBackViewController.h"

@interface SetUpViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSArray <NSArray <NSString *>*>*_titles;
    NSArray <NSArray <NSString *>*>*_sels;
}
@property(nonatomic,strong)UITableView * myTalbleView;

@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.gk_navTitle = @"设置";
    [self.view addSubview:self.myTalbleView];
    _titles = @[@[@"编辑信息"],
                @[@"密码设置",
                  @"手机号更换"],
                @[@"意见反馈",
                  @"清理缓存",
                  @"版本更新"]];
    _sels = @[@[NSStringFromSelector(@selector(editInfo))],
              @[NSStringFromSelector(@selector(settingPassword)),
                NSStringFromSelector(@selector(changePhoneNumber))],
              @[NSStringFromSelector(@selector(feedBack)),
                NSStringFromSelector(@selector(clearCash)),
                NSStringFromSelector(@selector(updateVersion))]];
}
-(UITableView*)myTalbleView
{
    if (_myTalbleView ==nil) {
        _myTalbleView =[[UITableView alloc] initWithFrame:CGRectMake(0,SKTopBar_Height ,SCREEN_WIDTH , SCREENH_HEIGHT-SKTopBar_Height) style:UITableViewStyleGrouped];
        _myTalbleView.dataSource = self;
        _myTalbleView.delegate = self;
        _myTalbleView.estimatedRowHeight = 0;
        _myTalbleView.estimatedSectionFooterHeight = 0;
        _myTalbleView.estimatedSectionHeaderHeight = 0;
        _myTalbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _myTalbleView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _myTalbleView;
}

#pragma mark -  UITableView 代理相关

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titles.count;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titles[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    for (id view in [cell.contentView subviews])
    {
        [view removeFromSuperview];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 200, 50)];
    label.textColor = SKColorWithHex(0x444444);
    label.font = SKFONT_Medium(15.0f);
    label.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:label];
    label.text = _titles[indexPath.section][indexPath.row];
    if (indexPath.section == 2 && indexPath.row > 0) {
        UILabel *label = [[UILabel alloc] init];
        label.font = SKFONT_SIZE(10);
        label.textColor = SKColorWithHex(0x999999);
        label.textAlignment = NSTextAlignmentRight;
        label.frame = CGRectMake(SCREEN_WIDTH - 15 - 100, 0, 100, 50);
        if (indexPath.row == 1) {
            // 内存
            [self appSizeBlock:^(NSInteger index) {
                label.text = [NSString stringWithFormat:@"%ldM", (long)index];
            }];
        }else if (indexPath.row == 2) {
            // 版本
            NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
            label.text = [NSString stringWithFormat:@"v%@版本", currentVersion];
        }
        [cell addSubview:label];
        return cell;
    }
    UIImageView * arrowImageView =[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -15 -6, (50-11)/2, 6, 11)];
    arrowImageView.image = SKIMAGE_NAMED(@"rightArrow");
    [cell.contentView addSubview:arrowImageView];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    if (section == 2) {
        UIView  * bgView = [UIView new];
        bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        UIFont * font = [UIFont systemFontOfSize:18];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((bgView.width -200)/2, bgView.height -44 , 200 , 44);
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        button.backgroundColor = SKColorWithHex(0xE33030);
        button.layer.cornerRadius = button.height/2;
        button.layer.masksToBounds = YES;
        [button setTitle:@"退出当前账号" forState:UIControlStateNormal];
        button.titleLabel.font = font ;
        [button setTitleColor:SKColorWithHex(0xFFFFFF) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(logOutDeal:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:button];
        return bgView;
    }
    return nil;
    
}
#pragma mark -- 退出登录
-(void)logOutDeal:(UIButton *)sender
{
    SKUserUserDefaults_Remove(@"UserInfo");
    [UserInfoSingleton shareInstance].userInfo = nil;
    SKPost_Notify(@"UserLogOut", nil, @{});
    self.tabBarController.selectedIndex = 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 200;
    }
    return 10;
    //    return CGFLOAT_MIN;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SEL sel = NSSelectorFromString(_sels[indexPath.section][indexPath.row]);
    if ([self respondsToSelector:sel]) {
        [self performSelector:sel withObject:nil afterDelay:0];
    }
}

#pragma mark -- mark SELECTER

- (void)editInfo {
    //TODO:个人资料
    MyPersonInfoVC * myPersonInfoVC = [MyPersonInfoVC new];
    [self.navigationController pushViewController:myPersonInfoVC animated:YES];
}
- (void)settingPassword {
    //TODO:密码修改
    if (![UserInfoSingleton shareInstance].userInfo.mobile) {
        [self showMessage:@"手机号为空"];
        return; }
    PassWordModifyVC * setPasVC = [PassWordModifyVC new];
    [self.navigationController pushViewController:setPasVC animated:YES];
}
- (void)changePhoneNumber {
    
}
- (void)feedBack {
    FeedBackViewController *vc = [[FeedBackViewController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}
- (void)clearCash {
    [self removeCashes];
    [self.myTalbleView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)updateVersion {
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
